import jinja2
from docxtpl import DocxTemplate
from public.settins import BASE_DIR,DAY,MONTH,DATATIME,DATA

CHECK_LIST_FILE = f"{BASE_DIR}/巡检清单.xlsx"
HOST_SHEET_NAME = "host"


def Get_Room_Host():
    from openpyxl import load_workbook

    wb = load_workbook(f'{BASE_DIR}/巡检清单.xlsx')
    room_sheet = wb['配电间']
    host_sheet = wb['host']
    room_max = room_sheet.max_row
    host_max = host_sheet.max_row
    rooms = [{"room": room_sheet.cell(i, 1).value} for i in range(2, room_max + 1)]
    hosts = [{"hostname": host_sheet.cell(i, 1).value, "host": host_sheet.cell(i, 2).value} for i in
             range(2, host_max + 1)]
    return rooms, hosts



# 巡检报告
def Out_Docx(device:list):
    rooms, hosts = Get_Room_Host()
    doc = DocxTemplate(f'{BASE_DIR}/网络设备巡检报告python模板20230607.docx')
    env = jinja2.Environment(autoescape=True)
    ctx = {"devices": device, "month": MONTH, "hosts": hosts, "rooms": rooms, "day": DAY}
    doc.render(context=ctx,jinja_env=env)
    doc.save(f"{BASE_DIR}/{DATA}/网络设备巡检报告{DATATIME}.docx")





def logs(msg: str):
    with open(f"{BASE_DIR}/conn.log", mode="a+", encoding="utf-8") as f:
        f.write(msg)


"""解析模板和巡检命令"""

Command_h3c = [
    {
        "man": {
            "config_commands": "\ndisplay device manuinfo",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },
    },
    {
        "device": {
            "config_commands": "\ndisplay device",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "clock": {
            "config_commands": "\ndisplay clock",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "version": {
            "config_commands": "\ndisplay version",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "fan": {
            "config_commands": "\ndisplay fan",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "cpu": {
            "config_commands": "\ndisplay cpu-usage",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "flash": {
            "config_commands": "\ndir flash:",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "boot": {
            "config_commands": "\ndisplay boot-loader",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "env": {
            "config_commands": "\ndisplay environment",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
    {
        "memory": {
            "config_commands": "\ndisplay memory",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify": False
        },

    },
]

Command_Huawei = [
    {
        "clock": {
            "config_commands": "\ndisplay clock",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },
    },
    {
        "version": {
            "config_commands": "\ndisplay version",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "env": {
            "config_commands": "\ndisplay temperature all",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "device": {
            "config_commands": "\ndisplay device",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "man": {
            "config_commands": "\ndisplay device manufacture-info",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "fan": {
            "config_commands": "\ndisplay fan",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "cpu": {
            "config_commands": "\ndisplay cpu-usage",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            "terminator": "TaskName"
            # "cmd_verify":False
        },

    },
    {
        # "boot": {
        #     "config_commands": "\ndisplay startup",
        #     "strip_command": False,
        #     "strip_prompt": False,
        #     "read_timeout": 30,
        #     "enter_config_mode": False,
        #     # "cmd_verify":False
        # },

    },
    {
        "memory": {
            "config_commands": "\ndisplay memory-usage",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
    {
        "flash": {
            "config_commands": "\ndir flash:",
            "strip_command": False,
            "strip_prompt": False,
            "read_timeout": 30,
            "enter_config_mode": False,
            # "cmd_verify":False
        },

    },
]

if __name__ == "__main__":
    # for item in Command_Huawei:
    #     print(list(item.keys())[0])
    print(BASE_DIR)